import { Dictionary, List } from 'lodash'
import * as _ from 'lodash'
import * as common from './common'
import userInfo from './userInfo'
import resource from './resource';
const log = console.log


import { cardsShower, handCardManager } from './cardManager'
export class userGameInfo{
    outCard: cardsShower
    Avatar: userInfo
    cardPrefab: cc.Prefab
    latsOutCardInfo = []
    leftCnt:cc.Node
    outText:cc.Label
    constructor(info ,position:string){
        this.Avatar=cc.instantiate(cc.find("Canvas").getComponent(resource).userInfo).getComponent(userInfo)
        this.Avatar.init(info,position)

        var leftCntPosition:cc.Vec2
        var outCardPosition:cc.Vec2
        switch(position){
            case 'left':    
                outCardPosition=cc.v2(-190,50)
                leftCntPosition = cc.v2(-320, 10)
            break;
            case 'center':  
                outCardPosition=cc.v2( 0,-80)
                leftCntPosition = cc.v2(1000, 1000)
            break;
            case 'right':   
                outCardPosition=cc.v2( 190,50) 
                leftCntPosition = cc.v2(320, 10)
            break;
        }
        this.outCard = new cardsShower(outCardPosition)
        this.leftCnt = cc.instantiate(cc.find("Canvas").getComponent(resource).cardleft)
        cc.find("Canvas").addChild(this.leftCnt)
        this.leftCnt.setPosition(leftCntPosition)

        this.leftCnt.active = false

        this.outText = this.showAt(outCardPosition, "pass")
        this.outText.node.active = false
    }
    playOut(userinfo){
        var cardinfo = userinfo.outCards
        if (!cardinfo) return
        if (!cardinfo.length) {
            this.playPass()
            return
        }
        var notsame = _.xor(cardinfo.map(item=>item.id), this.latsOutCardInfo.map(item=>item.id)).length
        this.latsOutCardInfo = cardinfo
        
        if(notsame){
            if(cardinfo.length){
                this.outCard.anchor.active = true
                this.outText.node.active = false
                this.outCard.display(cardinfo)
            } 
            this.leftCnt.getChildByName("number").getComponent(cc.Label).string = userinfo.handCards.length.toString()
        }
    }
    playPass(){
        this.outCard.anchor.active = false
        this.outText.node.active = true
        this.outText.string = 'pass'
    }
    prepared(){
        this.outCard.anchor.active = false
        this.outText.node.active = true
        this.outText.string = 'prepared'
    }
    grapCard(grap){
        if(grap>=0){
            this.outCard.anchor.active = false
            this.outText.node.active = true

            this.outText.string = grap?'grap':'pass'
        }
    }
    updateLeftCnt(userinfo){
        this.leftCnt.active = true
        this.leftCnt.getChildByName("number").getComponent(cc.Label).string = userinfo.handCards.length.toString()
    }
    destroy(){
        cc.find("Canvas").removeChild(this.leftCnt)
        cc.find("Canvas").removeChild(this.Avatar.node)
        this.outCard.destroy()

        this.outText.destroy()
    }
    showAt(position:cc.Vec2, text:string):cc.Label{
        var node = new cc.Node()
        var label = node.addComponent(cc.Label)
        label.font = cc.find("Canvas").getComponent(resource).font
        label.fontSize = 36
        label.string = text
        label.node.setPosition(position)
        cc.find("Canvas").addChild(label.node)
        return label
    }
}

import * as puke from './pukeData'
var that
export class roomManager{
    private static _ins:roomManager
    dzCard: cardsShower
    handCard: handCardManager
    usersInfo: Dictionary<userGameInfo> = {}
    roomInfo = {lastOut:null, dzid:null}
    static get(){
        if(this._ins==null) this._ins=new roomManager()
        return this._ins
    }
    constructor(){
        this.dzCard = new cardsShower(cc.v2(0, -35+cc.view.getVisibleSize().height/2))
        this.handCard = new handCardManager()
        that = this
    }
    receive(data){
        that.roomInfo = data.roomInfo
        switch(data.event){
            case 'joinPlayer':      that.joinPlayer(data.roomInfo.players); break;
            case 'preparePlayer':   that.preparePlayer(data.roomInfo.players); break;
            case 'handOutCards':    that.handOutCards(data.roomInfo.players); break;
            case 'grabCard':        that.grabCard(data.roomInfo.players); break;
            case 'grabCardRes':     that.grabCardRes(data.roomInfo); break;
            case 'playOut':         
                for(var uid in data.roomInfo.players)
                    that.usersInfo[uid].playOut(data.roomInfo.players[uid])
            break;
            case 'gameOver':        log("game over"); break;
        }
    }
    joinPlayer(users){
        let playerIdx = Object.keys(users).indexOf(common.uid)
        let usersid = Object.keys(users)
        for(let i=0;i<usersid.length;i++){
            let position
            if(i==playerIdx) position = 'center'
            else if(i==(playerIdx+2)%3) position = 'right'
            else position = 'left'
            if(this.usersInfo[usersid[i]]==undefined)
                this.usersInfo[usersid[i]] = new userGameInfo(users[usersid[i]],position)
        }
        this.preparePlayer(users)
        // _.xor(Object.keys(this.usersInfo), usersid).map(uid=>{
        //     this.usersInfo[uid].destroy()
        //     delete this.usersInfo[uid]
        // })
    }
    preparePlayer(users){
        for(var uid in users)
            if(users[uid].prepared)
                this.usersInfo[uid].prepared()
    }
    handOutCards(users){
        for(var uid in users){
            this.usersInfo[uid].outText.node.active=false
            this.usersInfo[uid].updateLeftCnt(users[uid])
            if(uid==common.uid)
                this.handCard.display(users[uid].handCards)
        }
        
    }
    grabCard(users){
        for(var uid in users) this.usersInfo[uid].grapCard(users[uid].grabRes)
    }
    grabCardRes(room){
        var users = room.players
        this.dzCard.display(room.dzCard)
        Object.keys(this.usersInfo).map(uid=>{
            this.usersInfo[uid].outText.node.active = false
        })
        if(room.dzid==common.uid) this.handCard.display(users[common.uid].handCards)
        else this.usersInfo[room.dzid].updateLeftCnt(users[room.dzid])
        this.usersInfo[this.roomInfo.dzid].Avatar.showHat()
    }
    userPlayOut(){
        
        var outCards = this.handCard.getPlayOut()
        var outStyle = puke.outCardCheck(outCards)
        if(!outStyle){
            common.alert('出牌不符合要求',this.handCard.anchor)
            return null
        }
        
        if(this.roomInfo.lastOut){
            var lastOutStyle = this.roomInfo.lastOut.outStyle
            if(lastOutStyle.type!=outStyle.type){
                if(outStyle.type!='炸弹'){
                    common.alert('出牌不符合要求',this.handCard.anchor)
                    return null
                }
            }
            else if(lastOutStyle.grade>=outStyle.grade){
                common.alert('出牌不符合要求',this.handCard.anchor)
                return null
            }
        }
        
        this.handCard.playOut()
        var param = {
            outStyle, outCards
        }
        return param
    }
    // lastPlay(users){
    //     var usersid = Object.keys(users)
    //     return usersid[(usersid.indexOf(uid)-1+3)%3]
    // }
}

